package tasks 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import utils.ErrorsCollector;
	import flash.events.IEventDispatcher;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class LoadResource extends BaseTask
	{
		private var _loader:Loader;
		private var _url:String;
		private var _userTag:String;
		protected var _taskRunning:Boolean = false;
		
		public function LoadResource(url:String, userTag:String) 
		{
			_url = url;
			_userTag = userTag;
		}
		
		override public function run():void 
		{
			// run only once
			if (!_taskRunning)
			{
				_taskRunning = true;
				loadResource(_url);
				super.run();
			}
		}
		
		private function loadResource(url:String):void
		{
			if (_loader != null)
			{
				return;
			}
			_loader = null;
			_loader = new Loader();
			configureListeners(_loader.contentLoaderInfo);
			
			try
			{
				_loader.load(new URLRequest(url)); 
			}
			catch(er:Error)
			{
				handleError(er.message);
				complete(false);
				return;
			}
		}
		
		private function handleError(err:String):void
		{
			ErrorsCollector.collectError(this + "Image loader Loader: " + err + " url: " + _url);
			complete(false);
		}
		
		// добавляет обработчики событий
		private function configureListeners(dispatcher:IEventDispatcher):void 
		{
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.COMPLETE, handleLoadComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
           /* dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);*/
        }
		
			
		// удаляет обработчики событий
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(Event.COMPLETE, handleLoadComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		private function handleLoadComplete(e:Event):void 
		{
			removeListeners(_loader.contentLoaderInfo);
			complete(true);
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void 
		{
            //handleError("httpStatusHandler: " + event);
        }

        private function initHandler(event:Event):void 
		{
            //handleError("initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
		{
			//ошибка загрузки
            handleError("Load image error: " + event.text);
        }

        private function openHandler(event:Event):void 
		{
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void 
		{
            //trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function unLoadHandler(event:Event):void 
		{
            //trace("unLoadHandler: " + event);
        }
		
		override public function destroy():void 
		{
			_loader = null;
			super.destroy();
		}
		
		public function get loader():Loader 
		{
			return _loader;
		}
		
		public function get url():String 
		{
			return _url;
		}
		
		public function get userTag():String 
		{
			return _userTag;
		}
		
		
		
	}

}